<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>店员编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
    <style type="text/css">
        .layui-form-item .layui-inline {
            width: 33.333%;
            float: left;
            margin-right: 0;
        }

        @media (max-width: 1240px) {
            .layui-form-item .layui-inline {
                width: 100%;
                float: none;
            }
        }
    </style>
</head>
<body class="childrenBody">
<form class="layui-form" style="padding: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">账号</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input username" lay-verify="required" name="username" placeholder="请输入用户名" readonly="readonly">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input"  name="name" lay-verify="required" placeholder="请输入姓名">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input"  name="phone" lay-verify="required" placeholder="请输入手机号码">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">选择角色</label>
        <div class="layui-input-block"  >
            <select name="roleId" lay-verify="required" id="roleId">
                <option value="">--请选择角色--</option>
            </select>
        </div>
    </div>

   <!-- <div class="layui-form-item">
        <label class="layui-form-label">选择门店</label>
        <div class="layui-input-block"  >
            <select name="merchantId" lay-verify="required" id="merchantId">
                <option value="">&#45;&#45;请选择门店&#45;&#45;</option>
            </select>
        </div>
    </div>-->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="editClerks">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--一般情况下，一个业务页面引入一个layui.js和业务js这两个两个js就够了-->
<!-- <script type="text/javascript" src="../../../lib/layui/layui.js"></script> -->
<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<!-- <script type="text/javascript" src="editMenu.js"></script> -->
<script type="text/javascript">
    layui.config({
        base: '/js/admin/'
    }).extend({
        ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
        $tool: 'tool'
    }).use(['form', 'layer', 'tree', 'jquery', 'ajaxExtention', '$tool'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : parent.layer,
            $ = layui.jquery,
            $tool = layui.$tool;
        $.ajax({
            type : "GET",
            url: '/merchants/merchants.json',
            dataType:'json',
            async:false,
            success: function(data){
                var list=data.data;
                var html='';
                for(var i=0;i<list.length;i++){
                    html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>"
                }
                $("#merchantId").append(html);
                form.render();
            }
        });

        $.ajax({
            type : "GET",
            url: '/roles/roles.json',
            dataType:'json',
            async:false,
            success: function(data){
                var list=data.data;
                var html='';
                for(var i=0;i<list.length;i++){
                    html += "<option value='"+list[i].id+"'>"+list[i].roledesc+"</option>"
                }
                $("#roleId").append(html);
                form.render();
            }
        });
        initClerkInfo();
        /**
         * 初始化用户信息
         * */
        function initClerkInfo() {
            var queryArgs = $tool.getQueryParam();//获取查询参数
            var id = queryArgs['id'];
            var url = '/users/getById';
            var req = {id:id};
            $.ajax({
                url:url,
                method:"POST",
                data:req,
                success:function (data) {
                    if(data.code==2000){
                        var datas = data.data;
                        $("input[name='username']").val(datas.username);
                        $("input[name='name']").val(datas.name);
                        $("input[name='phone']").val(datas.phone);
                        $("#roleId option[value='"+datas.roleId+"']").attr("selected","selected");
                        $("#merchantId option[value='"+datas.merchantId+"']").attr("selected","selected");
                        form.render();//重新绘制表单，让修改生效
                    }

                }
            });
        }

        /**
         * 表单提交
         * */
        form.on("submit(editClerks)", function (data) {
            var queryArgs = $tool.getQueryParam();//获取查询参数
            var id = queryArgs['id'];
            var name = data.field.name;
            var phone = data.field.phone;
            var roleId = data.field.roleId;
            var merchantId = data.field.merchantId;
            //请求
            var url ='/users';
            var req = {
                id:id,
                name:name,
                phone: phone,
                roleId:roleId,
                merchantId:merchantId
            };
            $.ajax({
                url: url,
                contentType:"application/json;charset=UTF-8",
                data: JSON.stringify(req),
                method: "PUT",
                success: function (data) {
                    //top.layer.close(index);(关闭遮罩已经放在了ajaxExtention里面了)
                    if(data.code==2000){
                        layer.msg("店员信息更新成功！", {time: 1000}, function () {
                            layer.closeAll("iframe");
                            //刷新父页面
                            parent.location.reload();
                        });
                    }
                }

            });

            return false;
        })

    });

</script>
</body>
</html>

